﻿using MyAlbum.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Collections.Generic;
using PersonalAlbum.Exceptions;
using MyAlbum.Model;
namespace UnitTests
{


    /// <summary>
    ///This is a test class for AlbumServiceTest and is intended
    ///to contain all AlbumServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AlbumServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _testConfig = new TestConfig();
            albums = new List<MyAlbum.Model.Album>();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            _testConfig.Dispose();
            foreach (var item in albums)
            {
                Service.DeleteItem(item);
            }
        }
        //
        #endregion
        private TestConfig _testConfig;
        private List<MyAlbum.Model.Album> albums;
        MyAlbum.Services.AlbumService Service
        {
            get
            {
                return ObjectFactory.GetInstance<IAlbumService>() as AlbumService;
            }
        }
        /// <summary>
        ///A test for UpdateItem
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void UpdateItem_NULL_Item()
        {
            var target = this.Service;
            target.UpdateItem(null, new MyAlbum.Utility.GenericValidationSummary());

        }

        ///</summary>
        [TestMethod()]
        public void UpdateItem_Bad_ID()
        {
            var target = this.Service;
            target.UpdateItem(new MyAlbum.Model.Album()
            {
                Id = -1 * new Random().Next(),
                Name = Guid.NewGuid().ToString("N")
            }, new MyAlbum.Utility.GenericValidationSummary());

        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void AddItem_Null_Item()
        {
            var target = this.Service;
            target.AddItem(null, new MyAlbum.Utility.GenericValidationSummary());
        }

        /// <summary>
        ///A test for UpdateItem
        ///</summary>
        [TestMethod()]
        public void UpdateItemTest()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);
            item.Name = Guid.NewGuid().ToString();
            item.Description = Guid.NewGuid().ToString();
            item.SectionId = new Random().Next();
            item.Url = Guid.NewGuid().ToString();
            target.UpdateItem(item, new MyAlbum.Utility.GenericValidationSummary());
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }


        /// <summary>
        ///A test for UpdateItem
        ///</summary>
        [TestMethod()]
        public void UpdateItem_Null_Name()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            string name = Guid.NewGuid().ToString();
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Name = name
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);
            item.Name = null;
            var v = new MyAlbum.Utility.GenericValidationSummary();
            target.UpdateItem(item, v);
            Assert.IsFalse(v.IsValid);
            Assert.AreEqual("Name", v.Errors.Keys.First());
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            //check that wasn't updated
            Assert.AreEqual(name, saved.Name);
        }

        [TestMethod()]
        public void UpdateItem_Null_Description()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            item.Name = Guid.NewGuid().ToString();
            item.Description = Guid.NewGuid().ToString();
            item.SectionId = new Random().Next();
            item.Url = Guid.NewGuid().ToString();
            target.UpdateItem(item, new MyAlbum.Utility.GenericValidationSummary());
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);

        }

        [TestMethod()]
        public void UpdateItem_Null_Url()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Description = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            item.Name = Guid.NewGuid().ToString();
            item.Description = Guid.NewGuid().ToString();
            item.SectionId = new Random().Next();
            item.Url = Guid.NewGuid().ToString();
            target.UpdateItem(item, new MyAlbum.Utility.GenericValidationSummary());
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);

        }


        /// <summary>
        ///A test for GetItems
        ///</summary>
        [TestMethod()]
        public void GetItems_Not_Visible_For_Admin()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }

        [TestMethod()]
        public void GetItems_Not_Visible_For_PowerUser()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            var user = new GenericUser("tal", new[] { "PowerUser" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }

        /// <summary>
        ///A test for GetItems
        ///</summary>
        [TestMethod()]
        public void GetItems_Not_Visible_For_Non_Admin_Non_User()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);
            user = new GenericUser("tal", new string[] { "" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNull(saved);
        }

        [TestMethod()]
        public void GetItems_Not_Visible_For_Non_Admin()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);
            var user = new GenericUser("tal", new[] { "User" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNull(saved);
        }

        /// <summary>
        ///A test for GetItems
        ///</summary>
        [TestMethod()]
        public void GetItems_Visible_For_Non_Admin()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now,
                Visible = true
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            user = new GenericUser() { Roles = new List<string>() { "User" } };
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }

        [TestMethod()]
        public void GetItems_Visible_For_Non_Admin_Non_User()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now,
                Visible = true
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);
            user = new GenericUser("tal", new string[] { "" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }
        /// <summary>
        ///A test for GetItemById
        ///</summary>
        [TestMethod()]
        public void GetItemByIdTest_Bad_ID()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var item = target.GetItemById(-1 * new Random().Next());
            Assert.IsNull(item);

        }
        /// <summary>
        ///A test for GetItemById
        ///</summary>
        [TestMethod()]
        public void GetItemByIdTest()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }

        /// <summary>
        ///A test for GetAlbumsBySectionId
        ///</summary>
        [TestMethod()]
        public void GetAlbumsBySectionId_Visible_Correct_Section_ID()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            string name = Guid.NewGuid().ToString("N");
            int section = new Random().Next(999, 999999);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Name = name,
                Timestamp = DateTime.Now,
                Description = name,
                Url = name,
                SectionId = section,
                Visible = true
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            MyAlbum.Model.Album item2 = new MyAlbum.Model.Album()
            {
                Name = name,
                Timestamp = DateTime.Now,
                Description = name,
                Url = name,
                SectionId = section,
                Visible = true
            };
            target.AddItem(item2, new MyAlbum.Utility.GenericValidationSummary());//so we'll have more than 1 album
            this.albums.Add(item2);

            var saved = target.GetAlbumsBySectionId(section);
            Assert.IsNotNull(saved);
            Assert.IsNotNull(saved.SingleOrDefault(s => s.Id == item.Id));
            Assert.IsTrue(saved.Count >= 2);
            foreach (var s in saved)
            {
                Assert.AreEqual(section, s.SectionId);
            }

        }

        /// <summary>
        ///A test for GetAlbumsBySectionId
        ///</summary>
        [TestMethod()]
        public void GetAlbumsBySectionId_Visible_Bad_Section_ID()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            string name = Guid.NewGuid().ToString("N");
            int section = new Random().Next(999, 999999);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Name = name,
                Timestamp = DateTime.Now,
                Description = name,
                Url = name,
                SectionId = section,
                Visible = true
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);
            user = new GenericUser("tal", new string[] { });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            var saved = target.GetAlbumsBySectionId(-1 * new Random().Next());
            Assert.IsNull(saved.SingleOrDefault(s => s.Id == item.Id));
        }

        /// <summary>
        ///A test for DeleteItem
        ///</summary>
        [TestMethod()]
        public void DeleteItem_Bad_ID()
        {
            var target = this.Service;
            target.DeleteItem(new MyAlbum.Model.Album() { Id = -1 * new Random().Next() });

        }

        /// <summary>
        ///A test for DeleteItem
        ///</summary>
        [TestMethod()]
        public void DeleteItemTest()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            string name = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Album album = new MyAlbum.Model.Album() { Name = name, Description = name, Timestamp = DateTime.Now };
            target.AddItem(album, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(album);


            var saved = target.GetItems().SingleOrDefault(a => a.Id == album.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(name, saved.Name);
            target.DeleteItem(saved);
            saved = target.GetItems().SingleOrDefault(a => a.Id == album.Id);
            Assert.IsNull(saved);
        }

        [TestMethod()]
        public void DeleteItemTest_Non_Admin()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            string name = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Album album = new MyAlbum.Model.Album() { Name = name, Description = name, Visible = true };
            target.AddItem(album, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(album);


            var saved = target.GetItems().SingleOrDefault(a => a.Id == album.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(name, saved.Name);
            user = new GenericUser("tal", new string[] { "" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            target.DeleteItem(saved);
            saved = target.GetItems().SingleOrDefault(a => a.Id == album.Id);
            Assert.IsNotNull(saved);
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItemTest()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }



        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItem_Null_Url()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Description = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.IsTrue(string.IsNullOrEmpty(saved.Url));
            Assert.AreEqual(item.Visible, saved.Visible);
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItem_Null_Description()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.albums.Add(item);

            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Name, saved.Name);
            Assert.AreEqual(item.SectionId, saved.SectionId);
            if (TestConfig._mode == PersistenceModes.Xml)
            {
                Assert.AreEqual(item.Timestamp, saved.Timestamp);
            }
            Assert.AreEqual(item.Url, saved.Url);
            Assert.AreEqual(item.Visible, saved.Visible);
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItem_Null_Name()
        {
            MyAlbum.Services.AlbumService target = this.Service;
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            var v = new MyAlbum.Utility.GenericValidationSummary();
            target.AddItem(item, v);
            this.albums.Add(item);
            Assert.IsFalse(v.IsValid);
            Assert.AreEqual("Name", v.Errors.Keys.First());

            //check that wasn't created
            Assert.AreEqual(0, item.Id);
        }

        /// <summary>
        ///A test for AlbumService Constructor
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void AlbumServiceConstructorTest()
        {
            MyAlbum.Data.IAlbumRepository albumRepository = null;
            MyAlbum.Services.AlbumService target = new MyAlbum.Services.AlbumService(albumRepository, null, null);
        }

    }
}
